package demo.socket.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ServerThread implements Runnable {
    
    private Socket socket;
    
    public ServerThread(Socket socket) {
        this.socket = socket;
    }
    
    @Override
    public void run() {
        
        InputStream is = null;
        try {
            // OutputStream os = null;
            byte[] b = new byte[1024];
            // os = socket.getOutputStream();
            is = socket.getInputStream();
            
            int n = is.read(b);
            
            String requeString = new String(b, 0, n);
            
            System.out.println("ServerThread.runrun[" + Thread.currentThread().getId() + "] 客户端读取[" + n + "]字节数据 : " + requeString);
            
        } catch (Exception e) {
            System.out.println("服务器端出现异常:" + e);
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                    System.out.println("ServerThread.run[" + Thread.currentThread().getId() + "]  关闭socket");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
